<?php
namespace app\admin\controller;
use think\view;
use think\Db;
use think\Session;
use Qiniu\Auth as Auth;
class Developer extends CommonController
{
    function show($status,$message,$data=array()){
        $reuslt = array(
            'status'=>$status,
            'message'=>$message,
            'data'=>$data,
        );
        exit(json_encode($reuslt)) ;   //输出一条信息 并退出当前脚本
    }
    // 全部技术
    public function index()
    {
        if(!empty($res['p'])){
            $p = $res['p'];
            $this->assign('p',$p);

        }

        $count = DB::name('developer')
            ->where('state','in','0,2')
            ->count(); //计算记录数
        $limitRows = 10; // 设置每页记录数
        $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

        $limit_value = $p->firstRow . "," . $p->listRows;
        $list = DB::name('developer')
            ->where('state','in','0,2')
            ->order('uptime desc')
            ->limit($limit_value)
            ->select();
        $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成


        $this->assign('list',$list);

        $this->assign('page',$page);
        return $this->fetch();
    }

    //审核技术
    public function check()
    {
        if(!empty($res['p'])){
            $p = $res['p'];
            $this->assign('p',$p);
        }

        $count = DB::name('developer')
            ->where(array(
                'state'=>1
            ))
            ->count(); //计算记录数
        $limitRows = 10; // 设置每页记录数
        $p = new AjaxPage($count, $limitRows,"news"); //第三个参数是你需要调用换页的ajax函数名

        $limit_value = $p->firstRow . "," . $p->listRows;
        $list = DB::name('developer')
            ->where(array(
                'state'=>1
            ))
            ->order('uptime desc')
            ->limit($limit_value)
            ->select();
        $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成


        $this->assign('list',$list);

        $this->assign('page',$page);
        return $this->fetch();
    }


    //编辑信息
    public function edit()
    {
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = Db::name('developer')->where($map)->find();
            $this->assign('list',$list);
        }
        if($_POST){
            $map['id']  = $_POST['id'];
            $_POST['uptime'] = date('Y-m-d H:i:s',time());
            $_POST['state'] = 2;
            $list = Db::name('developer')->where($map)->update($_POST);
            if($list){
                $this->success('审核成功', url('Developer/check'));

            }else{
                $this->error('审核失败');
            }
        }

        return $this->fetch();
    }

    //编辑技术信息
    public function developer_edit()
    {
        if($_GET){
            $map['id']  = $_GET['id'];
            $list = Db::name('developer')->where($map)->find();
            $this->assign('list',$list);
        }
        if($_POST){
            $map['id']  = $_POST['id'];
            $_POST['uptime'] = date('Y-m-d H:i:s',time());

            $list = Db::name('developer')->where($map)->update($_POST);
            if($list){
                $this->success('编辑成功', url('Developer/index'));

            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }


    //删除信息（只改变状态 不删除）
    public  function developer_del(){
        if($_POST){
            $map['id'] = $_POST['id'];
            $map['state'] = '-1';  //删除的状态
            $res = Db::name('developer')->update($map);
            if($res){
                return show(1,'删除成功');
            }else{
                return show(0,'删除失败');
            }
        }

    }

    /*
    * 禁用
    */
    public function yes(){
        $map['id'] = $_POST['id'];
        $_POST['state'] = 0;
        $res = Db::name('developer')->where($map)->update($_POST);;
        if($res){
            return show(1,'禁用成功！');
        }else{
            return show(0,'禁用失败！');
        }
    }
    /*
    * 解禁
    */
    public function no(){
        $map['id'] = $_POST['id'];
        $_POST['state'] = 2;
        $res = Db::name('developer')->where($map)->update($_POST);;
        if($res){
            return show(1,'解禁成功！');
        }else{
            return show(0,'解禁失败！');
        }
    }


    /**
     * 增加标签
     */
    public function tagadd(){
        $db_tags = Db::name('tags');
        $db_tag_category = DB::name('category');
        $tagsname = $db_tag_category->order('id ')->select();
        for($i=0;$i<count($tagsname);$i++){
            $tagsname[$i]['child'] = $db_tags->where(array(
                'father'=>$tagsname[$i]['id']
            ))->select();
        }
        $this->assign('tagsname', $tagsname);

        return $this->fetch();
    }

    /**
     * 减少标签
     */
    public function tagdel(){
        $db_tags = Db::name('tags');
        $db_tag_category = DB::name('category');
        $tagsname = $db_tag_category->order('id ')->select();
        for($i=0;$i<count($tagsname);$i++){
            $tagsname[$i]['child'] = $db_tags->where(array(
                'father'=>$tagsname[$i]['id']
            ))->select();
        }
        $this->assign('tagsname', $tagsname);
        return $this->fetch();
    }

    /**
     * 检查待审核技术数量
     */
    public function check_new(){
        //待审核订单数量显示
        $count = DB::name('developer')
            ->where(array(
                'state'=>'1'
            ))
            ->count(); //计算记录数
        $this->show(1,'获取成功',$count);
    }


    /**
     * 编辑标签
     */
    public function tagedit(){
        return $this->fetch();
    }



}
